#!/usr/bin/env python3
# _*_coding:utf-8 _*_
__author__ = "Gao"

from django.forms import ModelForm
from crm import models


class EnrollmentForm(ModelForm):
    '''报名流程'''

    def __new__(cls, *args, **kwargs):
        for field_name, field_obj in cls.base_fields.items():  # 给字段添加样式
            field_obj.widget.attrs['class'] = 'form-control'
        return ModelForm.__new__(cls)

    class Meta:
        model = models.Enrollment
        fields = ['enrolled_class', 'consultant']


class registriationForm(ModelForm):
    '''注册确认'''
    def __new__(cls, *args, **kwargs):
        for field_name, field_obj in cls.base_fields.items():  # 给字段添加样式
            field_obj.widget.attrs['class'] = 'form-control'
            if field_name in cls.Meta.readonly_fields:
                field_obj.widget.attrs['disabled'] = 'disabled'
        return ModelForm.__new__(cls)

    class Meta:
        model = models.Customer
        fields = '__all__'
        exclude = ['tags','content','memo','status','referral_form','consult_course']
        readonly_fields = ['qq','consultant','source']    # 自定义只读字段

    def clean_qq(self):
        if self.cleaned_data['qq'] != self.instance.qq:
            self.add_error('qq','该字段不能被修改')
        else:
            return self.cleaned_data['qq']

    def clean_consultant(self):
        if self.cleaned_data['consultant'] != self.instance.consultant:
            self.add_error('consultant','该字段不能被修改')
        else:
            return self.cleaned_data['consultant']

    def clean_source(self):
        if self.cleaned_data['source'] != self.instance.source:
            self.add_error('source','该字段不能被修改')
        else:
            return self.cleaned_data['source']

    def clean_qq_name(self):
        if not self.cleaned_data['qq_name'] or self.cleaned_data['qq_name'].strip() == '':
            self.add_error('qq_name','请填写QQ名')
        else:
            return self.cleaned_data['qq_name']

    def clean_phone(self):
        if not self.cleaned_data['phone'] or self.cleaned_data['phone'].strip()  == '':
            self.add_error('phone','请填写常用电话')
        else:
            return self.cleaned_data['phone']

    def clean_id_card(self):
        if not self.cleaned_data['id_card'] or self.cleaned_data['id_card'].strip() == '':
            self.add_error('id_card','请填写身份证号')
        else:
            return self.cleaned_data['id_card']

    def clean_email(self):
        if not self.cleaned_data['email'] or self.cleaned_data['email'].strip() == '':
            self.add_error('email','请填写常用邮箱')
        else:
            return self.cleaned_data['email']
